package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysCheckIn;
import com.ruoyi.system.service.ISysCheckInService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;

/**
 * 入住管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/checkIn")
public class SysCheckInController extends BaseController
{
    @Autowired
    private ISysCheckInService sysCheckInService;

    /**
     * 查询入住管理列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysCheckIn sysCheckIn)
    {
        startPage();
        List<SysCheckIn> list = sysCheckInService.selectSysCheckInList(sysCheckIn);
        return getDataTable(list);
    }

    /**
     * 获取入住管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysCheckInService.selectSysCheckInByCheckInId(id));
    }

    /**
     * 新增入住管理
     */
    @PostMapping("/add")
    @Log(title = "入住管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody SysCheckIn sysCheckIn)
    {
        return toAjax(sysCheckInService.insertSysCheckIn(sysCheckIn));
    }

    /**
     * 修改入住管理
     */
    @PostMapping("/edit")
    @Log(title = "入住管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody SysCheckIn sysCheckIn)
    {
        return toAjax(sysCheckInService.updateSysCheckIn(sysCheckIn));
    }

    /**
     * 删除入住管理
     */
    @PostMapping("/remove")
    @Log(title = "入住管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@RequestBody Long[] ids)
    {
        return toAjax(sysCheckInService.deleteSysCheckInByCheckInIds(ids));
    }

    /**
     * 审核入住申请
     */
     
    @Log(title = "入住审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{checkInId}")
    public AjaxResult audit(@PathVariable Long checkInId, @RequestBody SysCheckIn checkIn)
    {
        int i = sysCheckInService.auditSysCheckIn(checkInId, checkIn.getAuditStatus(),
                checkIn.getAuditRemark(), checkIn.getMonthlyFee());
        if (-2 == i){
            return AjaxResult.error("余额不足");
        }
        return toAjax(i);
    }

    /**
     * 导出入住管理列表
     */
    @PostMapping("/export")
    @Log(title = "入住管理", businessType = BusinessType.EXPORT)
    public AjaxResult export(@RequestBody SysCheckIn sysCheckIn)
    {
        List<SysCheckIn> list = sysCheckInService.selectSysCheckInList(sysCheckIn);
        ExcelUtil<SysCheckIn> util = new ExcelUtil<SysCheckIn>(SysCheckIn.class);
        return util.exportExcel(list, "checkIn");
    }

    /**
     * 根据老人用户ID查询入住记录
     */
    @GetMapping("/elder/{elderUserId}")
    public AjaxResult getCheckInByElder(@PathVariable("elderUserId") Long elderUserId)
    {
        List<SysCheckIn> list = sysCheckInService.selectSysCheckInByElderUserId(elderUserId);
        return AjaxResult.success(list);
    }

    /**
     * 办理退住
     */
     
    @Log(title = "办理退住", businessType = BusinessType.UPDATE)
    @PostMapping("/checkOut/{checkInId}")
    public AjaxResult checkOut(@PathVariable Long checkInId)
    {
        return toAjax(sysCheckInService.checkOut(checkInId));
    }
} 